package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dataTransmit.SetmealDTO;
import com.sky.dataTransmit.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.view.DishItemVO;
import com.sky.view.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    @Override
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));
            setmealDishMapper.insert(setmealDishes);
        }
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        List<Long> ids = Arrays.asList(setmealDTO.getId());
        setmealDishMapper.deleteByDishId(ids);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && !setmealDishes.isEmpty()){
            setmealDishes.forEach(setmealDish-> setmealDish.setDishId(setmealDTO.getId()));
            setmealDishMapper.insert(setmealDishes);
        }

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal=setmealMapper.getById(id);
            //如果菜品状态为启用则抛出异常
            if(setmeal.getStatus().equals(StatusConstant.ENABLE)) throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealDishMapper.deleteByDishId(ids);
        setmealMapper.delete(ids);
    }

    @Override
    public PageResult getList(SetmealPageQueryDTO setmealPageQueryDTO) {
        int page = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();
        PageHelper.startPage(page,pageSize);
        String name = setmealPageQueryDTO.getName();
        Integer status = setmealPageQueryDTO.getStatus();
        Integer categoryId = setmealPageQueryDTO.getCategoryId();
        Page<SetmealVO> page_= setmealMapper.getList(name,status,categoryId);
        return new PageResult(page_.getTotal(), page_.getResult());
    }

    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal= setmealMapper.getById(id);
        List<SetmealDish> setmealDishes= setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        new Dish();
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    //根据分类id查询套餐
    @Override
    public List<Setmeal> list(Long categoryId) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<Setmeal>()
                .eq(Setmeal::getCategoryId, categoryId)
                .eq(Setmeal::getStatus, StatusConstant.ENABLE);
        return list(queryWrapper);
    }

    //根据id查询包含的菜品
    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<SetmealDish>()
                .eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishMapper.selectList(queryWrapper);
        ArrayList<DishItemVO> dishItemVOS = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            DishItemVO dishItemVO = BeanUtil.copyProperties(setmealDish, DishItemVO.class);
            LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<Dish>()
                    .eq(Dish::getId, setmealDish.getDishId())
                    .select(Dish::getDescription)
                    .select(Dish::getImage);
            Dish dish = dishMapper.selectOne(queryWrapper1);
            dishItemVO.setDescription(dish.getDescription());
            dishItemVO.setImage(dish.getImage());
            dishItemVOS.add(dishItemVO);
        }
        return dishItemVOS;
    }
}
